package com.erp.framework.controller.taglib;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.erp.app.catalog.catgroup.vo.CatalogGroupVO;
import com.erp.app.common.ProjectConstants;
import com.erp.framework.util.user.User;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.util.CommonUtil;

public class CustomCatalogContentTab extends TagSupport {
    private String menuItemCollection;

    public String getMenuItemCollection() {
        return menuItemCollection;
    }

    public void setMenuItemCollection(String menuItemCollection) {
        this.menuItemCollection = menuItemCollection;
    }
    
 

    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        HttpSession session = pageContext.getSession();
        
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        

        Collection rslt = (Collection) pageContext.getAttribute(menuItemCollection);
        if (rslt == null) {
            throw new JspException("Cannot get Collection with property name " + menuItemCollection);
        }

        /*CustomCatalogScriptTag apexproMenuScriptTag = (CustomCatalogScriptTag) getParent();
        boolean hasItems[] = apexproMenuScriptTag.getHasItems();*/

        try {
            jspWriter.println("<table width=\"100%\" height=\"20\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" bgcolor=\"#FFFFFF\">");
//			jspWriter.println("<tr><td height=\"15\" bgcolor=\"#E9E1D9\"></td></tr>");

            int i = 0;
            int j = 1;
            int menuGroupIdx=1;
            Iterator iterator = rslt.iterator();
            while (iterator.hasNext()) {
                CatalogGroupVO menuGroupVO = (CatalogGroupVO) iterator.next();
                String idx = CommonUtil.bigDecimalToStr(menuGroupVO.getL01nodid());
                if (idx.startsWith("0") && idx.length() > 0) {
                    idx = idx.substring(1);
                }

                jspWriter.println("<tr bgcolor=\"#999999\" align=\"left\">");
                jspWriter.print("\t\t<td class=\"main_menu\" height=\"15\"  ");

                //jspWriter.print("onMouseOver=\"showSubMenu('elMenu" + menuGroupIdx + "\',"+"\'block\')\" onMouseOut=\"showSubMenu( 'elMenu" + menuGroupIdx + "\',"+"\'none\')\" ");
                jspWriter.print("onMouseOver=\"javascript:highlight(this, event, true);\""
                		+ " onMouseOut=\"javascript:highlight(this, event, false, '#999999');\" onClick=\"showSubMenu('elMenu" + menuGroupIdx + "\')\" style=\"cursor:hand\" ");

                //jspWriter.print("onMouseOver=\"showSubMenu('elMenu" + menuGroupIdx + "\',"+"\'block\')\"" );
                    //jspWriter.print("onClick=\"javascript:parent.frames.mainFrame.location='main.jsp?jsp/menuPage.jsp?menuGroup=" + menuGroupVO.getMenuGroup() + "';\"");
                    j++;
                    /*} else {
                    //get Url from MenuGroup
                    String uri = CommonUtil.nullToString(menuGroupVO.getGroupUri());
                    jspWriter.print("onMouseOver=\"javascript:highlight(this, '',event, true);\" ");
                    jspWriter.print("onMouseOut=\"javascript:highlight(this, '',event, false);\" ");
                    jspWriter.print("onClick=\"javascript:parent.frames.mainFrame.location='main.jsp?" + uri + "';\"");
                }*/

                jspWriter.print(">");
                //jspWriter.print("<a href=\"#\">");

                String menuGroupDesc = menuGroupVO.getL01dsc();
                String tmpMenuGroupDesc = null;

                if (!CommonUtil.isEmpty(tmpMenuGroupDesc)) {
                    menuGroupDesc = tmpMenuGroupDesc;
                }

                jspWriter.println("<font color=\"#000000\">" + menuGroupDesc + "</font></td>");
                jspWriter.println("</tr>");

                String historyPath = menuGroupDesc;
                String path = "<a href='catalogMainPage.do?catalogLevel="+menuGroupVO.getL01nodid().intValue()+"&historyPath="+URLEncoder.encode(historyPath)+"'>";
                path += menuGroupDesc;
                path += "</a>";
                ChildList itemList = menuGroupVO.getCatalogGroupChildsList(editingUser.getUserId());
                if (itemList != null && itemList.size() > 0) {
                     CreateSubMenu(jspWriter,"elMenu"+menuGroupIdx,itemList,menuGroupIdx,path, session);
                     menuGroupIdx++;
                }

            }

//			jspWriter.println("<tr><td height=\"15\" bgcolor=\"#E9E1D9\"></td></tr>");
        } catch (Exception e) {
            Logger.getLogger(this.getClass().getName()).error(e.getMessage(), e);
        } finally {
            try {
                jspWriter.println("</table>");
            } catch (Exception e) {
            }
        }

        return SKIP_BODY;
    }

    private void CreateSubMenu(JspWriter jspWriter,String  submenuname,ChildList itemList,int idx, String parentPath, HttpSession session) throws Exception{
         Iterator iter = itemList.iterator();
         int i=1;
         int submenuGroupIdx=1;
         User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
         while (iter.hasNext()) {
             Object obj = iter.next();
             String path = "";

             if (obj instanceof CatalogGroupVO) {
                 CatalogGroupVO itemVO = (CatalogGroupVO) obj;
                 ChildList subitemList = itemVO.getCatalogGroupChildsList(editingUser.getUserId());
                 if(subitemList.size() > 0){
                	 String historyPath = parentPath + ProjectConstants.STRING_SPACE + itemVO.getL01dsc();
                     path +=  parentPath + ProjectConstants.STRING_SPACE;
                     path += "<a href='itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue()+"&historyPath="+URLEncoder.encode(historyPath)+"'>";
                     path += itemVO.getL01dsc();
                     path += "</a>";
                	 jspWriter.println("<tr style=display:none id=\""+submenuname+(i++)+"\"+ bgcolor=\"#CCCCCC\" align=\"left\">");
                     jspWriter.print("\t\t<td class=\"subMenu\" height=\"15\" onMouseOver=\"javascript:highlight(this, event, true);\" onMouseOut=\"javascript:highlight(this, event, false, '#CCCCCC');\" ");
                     jspWriter.print(("onClick=\"showSubMenu('slMenu" +idx )+submenuGroupIdx + "\')\" style=\"cursor:hand\" >");
                    // jspWriter.print("<a class=\"style7\" href=\"itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue()+"\"  target=\"mainFrame\">");
                     jspWriter.println("<font color=\"#000000\">" + itemVO.getL01dsc() + "</font></td>");
                     jspWriter.println("</tr>");
                 }else{
                     path +=  parentPath + ProjectConstants.STRING_SPACE;
                     path += itemVO.getL01dsc();
                     jspWriter.println("<tr style=display:none id=\""+submenuname+(i++)+"\"+ bgcolor=\"#CCCCCC\" align=\"left\">");
                     jspWriter.print("\t\t<td class=\"subMenu\" height=\"15\" onMouseOver=\"javascript:highlight(this, event, true);\" onMouseOut=\"javascript:highlight(this, event, false, '#CCCCCC');\""
                    		 +" onClick=\"javascript:highlightItem(this,'itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue() +"&historyPath="+URLEncoder.encode(path)+"');\">");
                     //jspWriter.print(("onClick=\"showSubMenu('slMenu" +idx )+submenuGroupIdx + "\')\" style=\"cursor:hand\" >");
                     //jspWriter.print("<a class=\"style7\" href=\"itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue()+"\"  target=\"mainFrame\">");
                     jspWriter.println("<font color=\"#000000\">" + itemVO.getL01dsc() + "</font></td>");
                     jspWriter.println("</tr>");
                 }

                 if (subitemList != null && subitemList.size() > 0) {
                      CreateSubMenu2(jspWriter,("slMenu"+idx)+submenuGroupIdx,subitemList,itemVO.getL01nodid().intValue(),path, session);
                      submenuGroupIdx++;
                 }
            }
         }
    }

    private void CreateSubMenu2(JspWriter jspWriter,String  submenuname,ChildList itemList,int idx,String parentPath, HttpSession session) throws Exception{
        Iterator iter = itemList.iterator();
        int i=1;
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        while (iter.hasNext()) {
            Object obj = iter.next();

            if (obj instanceof CatalogGroupVO) {
                CatalogGroupVO itemVO = (CatalogGroupVO) obj;
                String path =  parentPath + ProjectConstants.STRING_SPACE;
                path += itemVO.getL01dsc();
                jspWriter.println("<tr style=display:none id=\""+submenuname+(i++)+"\"+ bgcolor=\"#CCCCCC\" align=\"left\">");
//                jspWriter.print("\t\t<td class=\"subMenu2\" height=\"15\" onMouseOver=\"javascript:highlight(this, event, true);\" onMouseOut=\"javascript:highlight(this, event, false);\" onClick=\"javascript:parent.frames.mainFrame.location='itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue()+"';\">" );
                jspWriter.print("\t\t<td class=\"subMenu2\" height=\"15\" onMouseOver=\"javascript:highlight(this, event, true);\" onMouseOut=\"javascript:highlight(this, event, false, '#E8E8E8' );\" onClick=\"javascript:highlightItem(this,'itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue()+"&historyPath="+URLEncoder.encode(path)+"');\">" );                
//                jspWriter.print("<a class=\"style7\" href=\"itemMasterSearch.do?action=search&seletStr=&srchArticleNo=&srchDesc=&srchnodid="+itemVO.getL01nodid().intValue()+"\"  target=\"mainFrame\">");
                jspWriter.println("<font color=\"#000000\">" + itemVO.getL01dsc() + "</font></td>");
                jspWriter.println("</tr>");
           }
        }
   }

	


}
